﻿using NetWorkToolkit;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 本地数据库
/// </summary>
public class PlayerMe
{
    #region Data

    private static PlayerMe m_Instance = null;

    private PlayerNetObject m_OwnerPlayerNetObj;

    /// <summary>
    /// 玩家网络实体集合
    /// </summary>
    private Dictionary<string, PlayerNetObject> PlayerDict = new Dictionary<string, PlayerNetObject>();

    /// <summary>
    /// 网络同步的模块集合
    /// </summary>
    private Dictionary<string, NetSyncModule> NetSyncModuleDic = new Dictionary<string, NetSyncModule>();

    /// <summary>
    /// 当前同步的模块列表集合，用于跟NetSyncModuleDic对比提出
    /// </summary>
    public List<string> CurSyncModuleList = new List<string>();

    private int playerCount;

    public int PlayerCount
    {
        get
        {
            return playerCount;
        }
    }

    public static PlayerMe Instance
    {
        get
        {
            if (m_Instance == null)
            {
                m_Instance = new PlayerMe();
            }
            return m_Instance;
        }
    }

    #endregion

    #region PlayerNetObject Data 

    public PlayerNetObject GetOwnerPlayerNetObj
    {
        get
        {
            if (m_OwnerPlayerNetObj == null)
            {
                foreach (var item in PlayerDict)
                {
                    if (item.Value.IsOwner)
                    {
                        m_OwnerPlayerNetObj = item.Value;
                        break;
                    }
                }
            }
            return m_OwnerPlayerNetObj;
        }
    }

    public PlayerNetObject GetPlayerNetObj(string playerid)
    {
        if (PlayerDict.ContainsKey(playerid))
        {
            return PlayerDict[playerid];
        }
        else
        {
            Debug.LogError("[LOG] Don't contain playerid");
            return null;
        }
    }

    public void AddPlayer(string playerid, PlayerNetObject pno)
    {
        if (!PlayerDict.ContainsKey(playerid))
        {
            PlayerDict.Add(playerid, pno);
            playerCount++;
        }
    }

    public void RemovePlayer(string playerid)
    {
        PlayerNetObject obj = null;
        if (PlayerDict.TryGetValue(playerid, out obj))
        {
            GameObject.Destroy(obj.gameObject);
            PlayerDict.Remove(playerid);
            playerCount--;
        }
        else
        {
            Debug.LogError("[LOG] Don't contain playerid");
        }
    }

    public void ClearPlayerData()
    {
        foreach (var item in PlayerDict.Values)
        {
            GameObject.Destroy(item.gameObject);
        }

        PlayerDict.Clear();
        playerCount = 0;
    }

    public void SetPlayerReadyStatus(string playerid, bool ready = true)
    {
        foreach (var item in PlayerDict)
        {
            if (item.Key == playerid)
            {
                item.Value.ready = ready;
            }
        }
    }

    public bool GetPlayerReadyStatus(string playerid)
    {
        foreach (var item in PlayerDict)
        {
            if (item.Key == playerid)
            {
                return item.Value.ready;
            }
        }

        Debug.LogError("[LOG] Don't contain this playerid");
        return false;
    }

    public bool GetAllPlayerReadyStatus()
    {
        foreach (var item in PlayerDict.Values)
        {
            if (!item.ready)
                return false;
        }
        return true;
    }


    #endregion

    #region NetSyncModule Data Manipulation



    public NetSyncModule GetNetModuleById(string id)
    {
        //return NetSyncModuleDic[id];
        NetSyncModule outModule;
        if (NetSyncModuleDic.TryGetValue(id, out outModule))
        {
            return outModule;
        }
        return null;
    }

    public void AddNetModule(string id, NetSyncModule module)
    {
        //if (NetSyncModuleDic.ContainsKey(id))
        //{

        //}
        NetSyncModuleDic[id] = module;
    }

    public void RemoveNetModule(string id)
    {
        if (NetSyncModuleDic.ContainsKey(id))
        {
            NetSyncModuleDic.Remove(id);
        }
        else
        {
            Debug.LogError("ERR RemoveNetModule is NULL !! id = " + id);
        }
    }

    public void RemoveAllNetSyncModuleDic()
    {
        NetSyncModuleDic.Clear();
    }

    #endregion


}
